package com.mo.lib.utils

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.location.LocationManager
import android.net.ConnectivityManager
import androidx.fragment.app.Fragment
import com.dylanc.longan.isLocationEnabled
import com.dylanc.longan.isNetworkAvailable

/**
 * @ author：mo
 * @ data：2022/4/20:9:44
 * @ 功能：广播工具类：https://www.jianshu.com/p/1e0a2aaa4d18
 */

/** Gps开关变化   别忘了GPS权限*/
const val ACTION_GPS = LocationManager.PROVIDERS_CHANGED_ACTION

/** 系统操作变化  暂时用来监听Home键及锁屏*/
const val ACTION_HOME = Intent.ACTION_CLOSE_SYSTEM_DIALOGS

/** 开屏*/
const val ACTION_SCREEN_ON = Intent.ACTION_SCREEN_ON

/** 锁屏*/
const val ACTION_SCREEN_OFF = Intent.ACTION_SCREEN_OFF

/** 解锁*/
const val ACTION_USER_PRESENT = Intent.ACTION_USER_PRESENT
/** 网络变化*/
const val ACTION_NET_CHANGE = ConnectivityManager.CONNECTIVITY_ACTION

/** 单例走一波*/
object BroadcastKtx {
    /**注册广播 */
    fun registerBroadcast(context: Context?, broadcastReceiver: BroadcastReceiver?, vararg action: String?) {
        if (context == null || broadcastReceiver == null) return
        val intentFilter = IntentFilter()
        for (element in action) {
            intentFilter.addAction(element)
        }
        context.registerReceiver(broadcastReceiver, intentFilter)
    }

    /** 解除注册广播，广播要和注册时是同一个*/
    fun unregisterBroadcast(context: Context?, broadcastReceiver: BroadcastReceiver?) {
        if (context == null || broadcastReceiver == null) return
        context.unregisterReceiver(broadcastReceiver)
    }
}

/** 注册广播*/
fun Context?.registerBroadcast(broadcastReceiver: BroadcastReceiver?, vararg action: String?) = BroadcastKtx.registerBroadcast(this, broadcastReceiver, *action)

/**解除注册广播，广播要和注册时是同一个 */
fun Context?.unregisterBroadcast(broadcastReceiver: BroadcastReceiver?) = BroadcastKtx.unregisterBroadcast(this, broadcastReceiver)

/** 注册广播*/
fun Fragment?.registerBroadcast(broadcastReceiver: BroadcastReceiver?, vararg action: String?) = BroadcastKtx.registerBroadcast(this?.context, broadcastReceiver, *action)

/**解除注册广播，广播要和注册时是同一个 */
fun Fragment?.unregisterBroadcast(broadcastReceiver: BroadcastReceiver?) = BroadcastKtx.unregisterBroadcast(this?.context, broadcastReceiver)

fun Context.registerBroadcast(vararg action: String?, onRecevive: (intent: Intent?) -> Unit = {}): BaseReceiver {
    val baseReceiver = BaseReceiver(onRecevive)
    registerBroadcast(baseReceiver, *action)
    return baseReceiver
}
fun Context.registerBroadcastGps(onRecevive: (intent: Intent?) -> Unit = {}): BaseReceiver? {
    return registerBroadcast(ACTION_GPS,onRecevive = onRecevive)
}
fun Context.registerBroadcastHome(onRecevive: (intent: Intent?) -> Unit = {}): BaseReceiver? {
    return registerBroadcast(ACTION_HOME,onRecevive = onRecevive)
}
fun Context.registerBroadcastScreen(onRecevive: (intent: Intent?) -> Unit = {}): BaseReceiver? {
    return registerBroadcast(ACTION_SCREEN_ON, ACTION_SCREEN_OFF, ACTION_USER_PRESENT,onRecevive = onRecevive)
}
fun Context.registerBroadcastNet(onRecevive: (intent: Intent?) -> Unit = {}): BaseReceiver? {
    return registerBroadcast(ACTION_NET_CHANGE,onRecevive = onRecevive)
}

/**当GPS开关变化 */
fun Intent.onGspChangeListener(onGspChange: (isOpen: Boolean) -> Unit = {}) {
    if (action.equals(ACTION_GPS)) {
        onGspChange.invoke(isLocationEnabled)
    }
}

/** 当Home键触发*/
fun Intent.onHomeTouchListener(onSysTouch: (state: Int) -> Unit = {}) {
    if (action.equals(ACTION_HOME)) {
        getStringExtra("reason")?.let {
            when (it) {
                "homekey"    -> { //短按Home键
                    onSysTouch.invoke(1)
                }
                "recentapps" -> { //多任务键(单home的时候长按)
                    onSysTouch.invoke(2)
                }
                "assist"     -> { //samsung 长按Home键
                    onSysTouch.invoke(3)
                }
            }
        }
    }
}

/** 当锁屏状态变化*/
fun Intent.onScreenListener(onScreenChange: (state: Int) -> Unit = {}) {
    action?.let {
        when (action) {
            ACTION_SCREEN_ON    -> { //开屏
                onScreenChange.invoke(1)
            }
            ACTION_SCREEN_OFF   -> { //锁屏
                onScreenChange.invoke(2)
            }
            ACTION_USER_PRESENT -> { //解锁
                onScreenChange.invoke(3)
            }
        }
    }
}
/**当GPS开关变化 */
fun Intent.onNetChangeListener(onNetChange: (isConnected: Boolean) -> Unit = {}) {
    if (action.equals(ACTION_NET_CHANGE)) {
        onNetChange.invoke(isNetworkAvailable)
    }
}

class BaseReceiver(private val onRecevive: (intent: Intent?) -> Unit) : BroadcastReceiver() {
    private var temp = 0L
    override fun onReceive(p0: Context?, p1: Intent?) {
//        val ms = DateUtils.getNowMills()
//        if (temp == 0L || ms - temp > 1000) {
//            temp = ms
            onRecevive.invoke(p1)
//        }
    }
}